package com.onesports.intelligent.k12.polarlight.service.wx;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.domain.dto.wx.CampusApplyDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.Message;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.MessageNotify;
import com.onesports.intelligent.k12.polarlight.domain.entity.wx.OrganizationCampusApply;
import com.onesports.intelligent.k12.polarlight.enums.MessageTypeEnum;
import com.onesports.intelligent.k12.polarlight.mapper.market.MessageMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.MessageNotifyMapper;
import com.onesports.intelligent.k12.polarlight.mapper.wx.OrganizationCampusApplyMapper;
import com.onesports.intelligent.k12.polarlight.util.BeanCopyUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.Instant;

/**
 * @author wangcx
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class CampusApplyService extends ServiceImpl<OrganizationCampusApplyMapper, OrganizationCampusApply> {

    private final OrganizationCampusApplyMapper mapper;
    private final MessageMapper messageMapper;
    private final MessageNotifyMapper messageNotifyMapper;

    /**
     * 社区申报
     *
     * @param dto 入参
     */
    @Transactional(rollbackFor = Exception.class)
    public void apply(CampusApplyDTO dto) {
        Instant now = Instant.now();
        OrganizationCampusApply entity = new OrganizationCampusApply();
        BeanCopyUtil.copyProperties(dto, entity);
        entity.setApplyTime(now);
        this.mapper.insert(entity);

        // 添加到消息表
        Message message = new Message();
        message.setTitle(MessageTypeEnum.CAMPUS_APPLY.getDesc());
        message.setMsgType(MessageTypeEnum.CAMPUS_APPLY.getValue());
        message.setContent(dto.getCampusName() + "，" + dto.getProperty() +"，"
                + dto.getCampusSpace() +  " 申报人联系电话：" + dto.getApplyPhone());
        message.setReceiverType(MessageTypeEnum.RECEIVER_TYPE_OP.getValue());
        messageMapper.insert(message);
        // 添加到消息通知表
        MessageNotify messageNotify = new MessageNotify();
        messageNotify.setMessageId(message.getId());
        messageNotify.setSendTime(Date.from(now));
        this.messageNotifyMapper.insert(messageNotify);


    }
}
